<?php
namespace Biz\service\Store;
use Biz\module\Classes\adm\StoreContentSearch;
use Doraemon\model\Member;
use Doraemon\model\Photos;
use Doraemon\service\Brand\BrandContentSearch;
use Doraemon\service\es\EsBrandContent;
use Doraemon\service\es\EsStoreContent;
use Doraemon\tools\BlockType;
use Doraemon\tools\Log;

class  SearchSchedule {

    public static function search(StoreContentSearch $request) {
        $params = [
            "page" => $request->page,
            "pagesize" => $request->page_size,
            "sort" => $request->sort
        ];

        // 门店id
        if ($request->store_id !== null) $params["store_id"] = $request->store_id;

        // 模块类型
        if ($request->content_module !== null) $params["content_module"] = $request->content_module;

        // 内容状态
        if ($request->content_status !== null) $params["content_status"] = $request->content_status;

        // 搜索词
        if (strlen($request->keyword)) $params["keyword"] = $request->keyword;

        // 内容类型
        if ($request->content_type) $params["content_type"] = $request->content_type;

        // 置顶
        if ($request->is_top) $params["is_top"] = $request->is_top;

        // 发布日期
        // 开始
        if (!empty($request->created_time_range["start"])) $params["created_time_start"] = $request->created_time_range["start"];
        // 结束
        if (!empty($request->created_time_range["end"])) $params["created_time_end"] = $request->created_time_range["end"];

        // sort way
        if ($request->desc) {
            $params["sort_way"] = $request->desc;
        }

        $rs = EsStoreContent::search($params);


        $total = $rs['total'] ?? 0;
        $list = $rs['rows'] ?? [];
        $is_over = $request->page_size * $request->page > $total ? 1 : 0;

        return compact('total', 'list', 'is_over');
    }
    
    /** 商家品牌后台 品牌认领 422
     * @param $brandId
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    public static function waitClaimList($params)
    {
        $return = array(
            "total" => 0,
            "is_over" => 1,
            "list" => array()
        );

        if (!$params['brand_id']) {
            return $return;
        }
        $params["fl"] = "id,like,favorite,fans,comment,owner_uid";
        $paramsObj = new  BrandContentSearch($params);

        $rs = self::handleWaitClaimList($paramsObj);
    
        if (is_array($rs) && $rs) {
            $return["total"] = $rs["total"];
            $return["list"] = $rs["rows"];
            $return["is_over"] = $params['page_size'] <= count($rs["rows"]) ? 0 : 1;
        }

        return $return;
    }


    /** 商家品牌后台 品牌认领 处理条件
     * @param $brandId
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    public static function handleWaitClaimList(BrandContentSearch $paramsObj)
    {
        $params = array(
            "page" => $paramsObj->page,
            "pagesize" => $paramsObj->page_size,
            "brand_id" => $paramsObj->brand_id,
        );

        // 排序方式
        if ($paramsObj->sort_type !== null) {
            switch ($paramsObj->sort_type) {
                case BrandContentSearch::SORT_TYPE_ZAN:
                    $params["sort"] = "like";
                    break;
                case BrandContentSearch::SORT_TYPE_FAV:
                    $params["sort"] = "favorite";
                    break;
                case BrandContentSearch::SORT_TYPE_COM:
                    $params["sort"] = "comment";
                    break;
                case BrandContentSearch::SORT_TYPE_FAN:
                    $params["sort"] = "fans";
                    break;
                case BrandContentSearch::SORT_TYPE_TIME_DESC:
                    $params["sort"] = "addtime";
                    break;
                case BrandContentSearch::SORT_TYPE_TIME_ASC:
                    $params["sort"] = "addtime";
                    $params["sort_type"] = "asc";
                    break;
            }
        }

        if ($paramsObj->fl !== null) {
            $params["fl"] = $paramsObj->fl;
        }

        if ($paramsObj->brand_name !== null) {
            $params["brand_name"] = $paramsObj->brand_name;
        }

        if (is_array($paramsObj->brand_alias) && $paramsObj->brand_alias) {
            $params["brand_alias"] = implode(",", $paramsObj->brand_alias);
        }

        if (is_array($paramsObj->search_keywords) && $paramsObj->search_keywords) {
            $params["search_keywords"] = implode(",", $paramsObj->search_keywords);
        }

        // 商品类型
        if ($paramsObj->goods_type !== null) {
            switch ($paramsObj->goods_type) {
                case BrandContentSearch::GOODS_TYPE_SAME:
                    $params["is_similar_goods"] = 1;
                    break;
                case BrandContentSearch::GOODS_TYPE_DIFF:
                    $params["is_similar_goods"] = 0;
                    break;
            }
        }

        // 内容类型
        if ($paramsObj->content_type !== null) {
            switch ($paramsObj->content_type) {
                case BrandContentSearch::CONTENT_TYPE_PIC:
                    $params["obj_type"] = BlockType::TYPE_PHOTO;
                    $params["show_type"] = Photos::SHOW_TYPE_PHOTO;
                    break;
                case BrandContentSearch::CONTENT_TYPE_VIDIO:
                    $params["obj_type"] = BlockType::TYPE_PHOTO;
                    $params["show_type"] = Photos::SHOW_TYPE_VIDEO;
                    break;
                case BrandContentSearch::CONTENT_TYPE_NOTE:
                    $params["obj_type"] = BlockType::TYPE_BLANK;
                    break;
            }
        }

        // 用户类型
        if (is_array($paramsObj->user_type) && $paramsObj->user_type) {
            $userTypes = array();
            foreach ($paramsObj->user_type as $userType) {
                switch ($userType) {
                    case BrandContentSearch::USER_TYPE_USER:
                        array_push($userTypes, Member::USER_COMMON);
                        break;
                    case BrandContentSearch::USER_TYPE_DESIGNER:
                        array_push($userTypes, Member::AUTH_USER_DESIGNER);
                        break;
                    case BrandContentSearch::USER_TYPE_BRAND:
                        array_push($userTypes, Member::AUTH_USER_BRAND);
                        break;
                }
            }

            if (is_array($userTypes) && $userTypes) {
                $params["user_type"] = implode(",", $userTypes);
            }
        }

        $rs = EsBrandContent::waitClaimList($params);

        return $rs;
    }
}